﻿#include "Yuan.h"
#include <QPixmap>

QCursor Yuan::CustomCursor()
{
    static const unsigned char YCursorBits[] = {
        0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,  0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52
        ,0x00, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00, 0x2D,  0x08, 0x06, 0x00, 0x00, 0x00, 0x52, 0xE9, 0x60
        ,0xA2, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59,  0x73, 0x00, 0x00, 0x0B, 0x13, 0x00, 0x00, 0x0B
        ,0x13, 0x01, 0x00, 0x9A, 0x9C, 0x18, 0x00, 0x00,  0x01, 0x40, 0x49, 0x44, 0x41, 0x54, 0x58, 0x85
        ,0xED, 0xD5, 0x21, 0x6E, 0xC3, 0x30, 0x14, 0xC6,  0xF1, 0xFF, 0x9B, 0xC6, 0x36, 0x30, 0x38, 0xA9
        ,0x05, 0x01, 0x05, 0x81, 0x05, 0x03, 0x39, 0xCA,  0x60, 0x8F, 0xD2, 0x03, 0xEC, 0x10, 0x3B, 0x46
        ,0xC1, 0xC0, 0xC6, 0x0A, 0x3B, 0x96, 0xB1, 0x80,  0x82, 0xC1, 0x56, 0x2A, 0xFF, 0x06, 0xE2, 0x36
        ,0x75, 0x9A, 0xB4, 0xCA, 0xEC, 0x4E, 0x9A, 0xE4,  0x2F, 0xB2, 0x42, 0x22, 0xFF, 0xF2, 0xFC, 0x9C
        ,0x18, 0x52, 0x52, 0x52, 0x52, 0x52, 0x52, 0x52,  0x52, 0x52, 0x52, 0xFE, 0x55, 0xE4, 0xC6, 0xA0
        ,0xDC, 0xC4, 0x71, 0x87, 0xC1, 0xC1, 0x68, 0x01,  0xCC, 0x06, 0xC2, 0x51, 0xD0, 0x29, 0xB0, 0x18
        ,0x00, 0xDF, 0xC6, 0x40, 0x33, 0x37, 0x84, 0x30,  0x4C, 0x80, 0x85, 0xCE, 0x7B, 0x2E, 0x2A, 0x91
        ,0x84, 0x24, 0xBE, 0x25, 0xDE, 0x25, 0x5E, 0x2F,  0x6E, 0xAE, 0xD0, 0x37, 0x92, 0x10, 0xF0, 0x09
        ,0x54, 0x40, 0xE9, 0xEE, 0x15, 0xC6, 0xA2, 0x77,  0xFE, 0xE0, 0xE5, 0x85, 0x8F, 0x16, 0x58, 0xDF
        ,0x35, 0x06, 0x5B, 0xD3, 0xB9, 0xD4, 0x11, 0xD0,  0xA5, 0x8F, 0xDE, 0x57, 0x75, 0x83, 0x73, 0x50
        ,0x06, 0xF6, 0x72, 0x0A, 0x47, 0x40, 0x57, 0x0D,  0x38, 0xDE, 0xC0, 0x04, 0x6F, 0x68, 0x05, 0x36
        ,0xF5, 0xE1, 0x08, 0x3D, 0xCD, 0xEA, 0xEA, 0x5A,  0xD8, 0xBE, 0x5A, 0x46, 0xB0, 0x05, 0x1E, 0xAC
        ,0xF1, 0xC2, 0xD1, 0xCC, 0x01, 0x6D, 0x74, 0x02,  0xDB, 0x3B, 0xBF, 0xD3, 0x73, 0x07, 0x87, 0x2F
        ,0xEF, 0x53, 0x07, 0x38, 0x82, 0x2F, 0xF6, 0xFB,  0xB8, 0x81, 0x73, 0x41, 0x69, 0x28, 0x3A, 0x7A
        ,0x5C, 0xDD, 0x73, 0xCF, 0x3A, 0x86, 0xA3, 0x05,  0x87, 0xEA, 0xCC, 0x60, 0xA1, 0x06, 0x75, 0x89
        ,0xFE, 0x77, 0x92, 0x76, 0x68, 0x23, 0xEF, 0x88,  0xD3, 0x4C, 0xA8, 0x10, 0x7A, 0xD4, 0xEF, 0x8E
        ,0xBE, 0x8B, 0x68, 0x79, 0x3A, 0xB1, 0x72, 0xE1,  0xAE, 0xBC, 0x13, 0x0D, 0xDE, 0xBD, 0x3D, 0xF3
        ,0x08, 0x15, 0xD4, 0xDF, 0x4C, 0x06, 0x36, 0xF7,  0x9E, 0x09, 0xED, 0xE9, 0x99, 0x97, 0x3E, 0x42
        ,0xFF, 0x30, 0x42, 0x4B, 0xA1, 0x8D, 0xD8, 0xE9,  0x2A, 0xBD, 0xED, 0x41, 0x25, 0x2A, 0x89, 0x37
        ,0x1F, 0xBD, 0xEA, 0x61, 0x8B, 0x5F, 0xDD, 0xC1,  0xFA, 0x01, 0xD8, 0xA3, 0x8F, 0xFB, 0xCA, 0x70
        ,0x16, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45,  0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82
    };

    static QCursor cursor = QCursor(QPixmap::fromImage(QImage::fromData(YCursorBits, sizeof(YCursorBits))), 15, 23);
    return cursor;
    //    return Qt::CrossCursor;
}

Yuan::Yuan()
{

}

#ifdef Q_OS_WIN

#include <qt_windows.h>
#include <QPushButton>
#include <QTimer>
QRect validRect(const QPoint &pos)
{
    POINT p;
    p.x = pos.x();
    p.y = pos.y();
    HWND wid = ::WindowFromPoint(p);
    QRect geometry;
    if(wid){
        RECT rect;
        if (::GetWindowRect(HWND(wid), &rect)) {
            geometry.setTop(rect.top);
            geometry.setBottom(rect.bottom);
            geometry.setLeft(rect.left);
            geometry.setRight(rect.right);
        }
    }
    return geometry;
}
QWidget* triggerWidget(std::function<void()> func, QWidget *parent)
{
    QPushButton* btn = new QPushButton("开始");
    btn->setFixedSize(100, 30);
    btn->connect(btn, &QPushButton::clicked, [func, parent]{
        parent->hide();
        QTimer::singleShot(300, func);
    });
    return btn;
}
#else
/*
 * Qy系列是我开源的，单独的Y打头文件是朋友合作写的，YLIBRARY系列是朋友自己的，没法开源
 * 其实也没用到啥，就是通过坐标查找窗口和系统快捷键，都很好实现
 * 坐标查找窗口原理:获取桌面所有窗口，获取他们的Geometry，然后和鼠标位置做contains判断
 * 系统快捷键就更好替代了，开源库qxtglobalshortcut，完美
*/
#ifdef has_YLIBRARY
#include "YWindowInfo.h"
#include "YShortcut.h"
#include "YShortcutEdit.h"
#include <QTimer>

QRect validRect(const QPoint &pos)
{
    return YWindowInfo::windowAt(pos, true, false);
}
QWidget* triggerWidget(std::function<void()> func, QWidget *parent)
{
    YShortcutEdit* se = new YShortcutEdit(parent);
    se->setFixedSize(100, 30);
    se->connect(se, &YShortcutEdit::shortcutChanged, [func, parent](YShortcut* shortcut){
        shortcut->connect(shortcut, &YShortcut::trigger, [func, parent]{
            parent->hide();
            QTimer::singleShot(300, func);
        });
    });
    se->setShortcut(new YShortcut(Qt::Key_Y));
    return se;
}
#else
#include <QPushButton>
#include <QTimer>
QRect validRect(const QPoint &pos)
{
    Q_UNUSED(pos);
    return QRect();
}
QWidget* triggerWidget(std::function<void()> func, QWidget *parent)
{
    QPushButton* btn = new QPushButton("开始");
    btn->setFixedSize(100, 30);
    btn->connect(btn, &QPushButton::clicked, [func, parent]{
        parent->hide();
        QTimer::singleShot(300, func);
    });
    return btn;
}
#endif
#endif


QRect Yuan::validRect()
{
    return ::validRect(QCursor::pos());
}

QWidget *Yuan::triggerWidget(std::function<void()> func, QWidget *parent)
{
    return ::triggerWidget(func, parent);
}
